#include <stdlib.h>
#include "qm.h"

template <class T> void Mat_copy(int m, int n, T src, int ldsrc, T dst, int lddst) {
    for(int j = 0; j < n; ++j) {
	for(int i = 0; i < m; ++i) {
	    dst[i+j*lddst] = src[i+j*ldsrc] ;
	}
    }
}

template <class T> void Mat_block(int mb, int nb, int *imb, int *inb, T A, int ldA) {

    // identify the largest column block and allocate memory
    int mbsize = inb[1] - inb[0];
    for(int i = 1; i < nb - 1; ++i) {
	if(inb[i+1] - inb[i] > mbsize) mbsize = inb[i+1] - inb[i];
    }
    double *cblock = (double*)malloc(mbsize*ldA*sizeof(double));

    // inline block alignment
    for(int j = 0, jj = 0; j < nb; ++j) {
	int inbj = inb[j+1] - inb[j];
        copyv(imb[mb-1], inbj, &A[jj], ldA, cblock, imb[mb-1]);
	for(int i = 0, ii = jj; i < mb; ++i) {
	    int imbi = imb[i+1] - imb[i];
	    copyv(imbi, inbj, cblock, imb[mb-1], &A[ii], imbi);
	    ii += imbi*inbj;
	}
	jj += ldA*inbj;
    }
}
